home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / congrid.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  141 lines

  1. ; $Id: congrid.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1988-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6. ;+
  7. ; NAME:
  8. ;    CONGRID
  9. ;
  10. ; PURPOSE:
  11. ;       Shrink or expand the size of an array by an arbitrary amount.
  12. ;       This IDL procedure simulates the action of the VAX/VMS
  13. ;       CONGRID/CONGRIDI function.
  14. ;
  15. ;    This function is similar to "REBIN" in that it can resize a
  16. ;       one, two, or three dimensional array.   "REBIN", however,
  17. ;       requires that the new array size must be an integer multiple
  18. ;       of the original size.   CONGRID will resize an array to any
  19. ;       arbitrary size (REBIN is somewhat faster, however).
  20. ;       REBIN averages multiple points when shrinking an array,
  21. ;       while CONGRID just resamples the array.
  22. ;
  23. ; CATEGORY:
  24. ;       Array Manipulation.
  25. ;
  26. ; CALLING SEQUENCE:
  27. ;    array = CONGRID(array, x, y, z)
  28. ;
  29. ; INPUTS:
  30. ;       array:  A 1, 2, or 3 dimensional array to resize.
  31. ;               Data Type : Any type except string or structure.
  32. ;
  33. ;       x:      The new X dimension of the resized array.
  34. ;               Data Type : Int or Long (greater than or equal to 2).
  35. ;
  36. ; OPTIONAL INPUTS:
  37. ;       y:      The new Y dimension of the resized array.   If the original
  38. ;               array has only 1 dimension then y is ignored.   If the
  39. ;               original array has 2 or 3 dimensions then y MUST be present.
  40. ;
  41. ;       z:      The new Z dimension of the resized array.   If the original
  42. ;               array has only 1 or 2 dimensions then z is ignored.   If the
  43. ;               original array has 3 dimensions then z MUST be present.
  44. ;
  45. ; KEYWORD PARAMETERS:
  46. ;       INTERP: If set, causes linear interpolation to be used.
  47. ;               Otherwise, the nearest-neighbor method is used.
  48. ;
  49. ;    CUBIC:    If specified and non-zero, "Cubic convolution"
  50. ;        interpolation is used.  This is a more
  51. ;        accurate, but more time-consuming, form of interpolation.
  52. ;        CUBIC has no effect when used with 3 dimensional arrays.
  53. ;        If this parameter is negative and non-zero, it specifies the
  54. ;        value of the cubic interpolation parameter as described
  55. ;        in the INTERPOLATE function.  Valid ranges are -1 <= Cubic < 0.
  56. ;        Positive non-zero values of CUBIC (e.g. specifying /CUBIC)
  57. ;        produce the default value of the interpolation parameter
  58. ;        which is -1.0.
  59. ;
  60. ;       MINUS_ONE:
  61. ;               If set, will prevent CONGRID from extrapolating one row or
  62. ;               column beyond the bounds of the input array.   For example,
  63. ;               If the input array has the dimensions (i, j) and the
  64. ;               output array has the dimensions (x, y), then by
  65. ;               default the array is resampled by a factor of (i/x)
  66. ;               in the X direction and (j/y) in the Y direction.
  67. ;               If MINUS_ONE is present (AND IS NON-ZERO) then the array
  68. ;               will be resampled by the factors (i-1)/(x-1) and (j-1)/(y-1).
  69. ;
  70. ; OUTPUTS:
  71. ;    The returned array has the same number of dimensions as the original
  72. ;       array and is of the same data type.   The returned array will have
  73. ;       the dimensions (x), (x, y), or (x, y, z) depending on how many
  74. ;       dimensions the input array had.
  75. ;
  76. ; PROCEDURE:
  77. ;       IF the input array has three dimensions, or if INTERP is set,
  78. ;       then the IDL interpolate function is used to interpolate the
  79. ;       data values.
  80. ;       If the input array has two dimensions, and INTERP is NOT set,
  81. ;       then the IDL POLY_2D function is used for nearest neighbor sampling.
  82. ;       If the input array has one dimension, and INTERP is NOT set,
  83. ;       then nearest neighbor sampling is used.
  84. ;
  85. ; EXAMPLE:
  86. ;       ; vol is a 3-D array with the dimensions (80, 100, 57)
  87. ;       ; Resize vol to be a (90, 90, 80) array
  88. ;       vol = CONGRID(vol, 90, 90, 80)
  89. ;
  90. ; MODIFICATION HISTORY:
  91. ;       DMS, Sept. 1988.
  92. ;       DMS, Added the MINUS_ONE keyword, Sept. 1992.
  93. ;     Daniel Carr. Re-wrote to handle one and three dimensional arrays
  94. ;                    using INTERPOLATE function.
  95. ;     DMS, RSI, Nov, 1993.  Added CUBIC keyword.
  96. ;-
  97.  
  98. FUNCTION CONGRID, arr, x, y, z, Interp=int, Minus_One=m1, Cubic = cubic
  99.  
  100. ON_ERROR, 2        ;Return to caller if error
  101. s = Size(arr)
  102.  
  103. IF ((s[0] EQ 0) OR (s[0] GT 3)) THEN $
  104.    Message, 'Array must have 1, 2, or 3 dimensions.'
  105.  
  106. ;  Supply defaults = no interpolate, and no minus_one.
  107. if n_elements(int) le 0 then int = 0 else int = keyword_set(int)
  108. if n_elements(m1) le 0 then m1 = 0 else m1 = keyword_set(m1)
  109. if n_elements(cubic) eq 0 then cubic = 0
  110. if cubic ne 0 THEN int = 1    ;Cubic implies interpolate
  111.     
  112.  
  113. CASE s[0] OF
  114.    1: BEGIN                ; *** ONE DIMENSIONAL ARRAY
  115.     srx = float(s[1] - m1)/(x-m1) * findgen(x)  ;subscripts
  116.       IF int THEN $
  117.          RETURN, INTERPOLATE(arr, srx, CUBIC = cubic) ELSE $
  118.          RETURN, arr[ROUND(srx)]
  119.    ENDCASE
  120.    2: BEGIN ; *** TWO DIMENSIONAL ARRAY
  121.     IF int THEN BEGIN
  122.       srx = float(s[1] - m1) / (x-m1) * findgen(x)
  123.       sry = float(s[2] - m1) / (y-m1) * findgen(y)
  124.           RETURN, INTERPOLATE(arr, srx, sry, /GRID, CUBIC=cubic)
  125.     ENDIF ELSE $
  126.       RETURN, POLY_2D(arr, $
  127.         [[0,0],[(s[1]-m1)/float(x-m1),0]], $ ;Use poly_2d
  128.         [[0,(s[2]-m1)/float(y-m1)],[0,0]],int,x,y)
  129.  
  130.    ENDCASE
  131.    3: BEGIN ; *** THREE DIMENSIONAL ARRAY
  132.     srx = float(s[1] - m1) / (x-m1) * findgen(x)
  133.     sry = float(s[2] - m1) / (y-m1) * findgen(y)
  134.     srz = float(s[3] - m1) / (z-m1) * findgen(z)
  135.     RETURN, interpolate(arr, srx, sry, srz, /grid)
  136.    ENDCASE
  137. ENDCASE
  138.  
  139. RETURN, arr_r
  140. END
  141.